<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <!-- 插入到left插槽位置 -->
        <template #left>
          <span>本月: 社保在缴 公积金在缴</span>
        </template>
        <!-- 插入到right插槽位置 -->
        <template #right>
          <el-button type="warning" size="small">导入excel</el-button>
          <el-button type="danger" size="small">导出excel</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>
      <el-card style="margin-top: 10px;">
        <el-table border :data="EmployeeList" :default-sort="{prop: 'timeOfEntry', order: 'ascending'}">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" sortable prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{row}">
              {{ EmployeeEnumFuction(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态" /> -->
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <!-- <el-button type="text" size="small" @click="$router.push(`/employees/detail?id=${row.id}`)">查看</el-button> -->
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-sizes="[2, 3, 5, 10,20]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <!-- 弹窗组件 -->
      <el-dialog title="新增员工" :visible="showDialog" @close="hCloseDialog">
        <addorEdit ref="AddEmployee" @close="showDialog=false" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import PageTools from '@/components/PageTools'
//
import EmployeeEnum from '@/constant/employees'
const hireType = EmployeeEnum.hireType.reduce((acc, item) => {
  acc[item.id] = item.value
  return acc
}, {})

// 导入弹窗组件
import addorEdit from './empDialog'
console.log('hireType', hireType)
export default {
  components: {
    PageTools, addorEdit
  },
  data() {
    return {
      showDialog: false, // 默认弹层
      page: 1,
      size: 3,
      total: 0, // 获取的总数据
      EmployeeList: []// 详情数据
    }
  },
  created() {
    this.loadgetEmployeeList()
  },
  methods: {
    // 删除功能
    async hDel(id) {
      try {
        const res = await delEmployee(id)
        console.log('delEmployee', res)
        if (res.code !== 10000) return // 失败
        // 成功
        // 某个列表的数据
        if (this.EmployeeList.length === 1) {
          this.page--
          if (this.page <= 0) {
            this.page = 1
          }
        }

        //
        this.loadgetEmployeeList()
      } catch (error) {
        console.log(error)
      }
    },
    //
    hCloseDialog() {
      this.$refs.AddEmployee.resetForm()

      //
      this.showDialog = false
    },
    //
    hSuccess() {
      // 因为ajax是异步的,total的值是当前的
      this.total++
      // 获取最大的页面
      const maxPage = Math.ceil(this.total / this.size)
      this.page = maxPage
      // 渲染
      this.loadgetEmployeeList()
    },
    // 枚举
    EmployeeEnumFuction(data) {
      return hireType[data] || '未知'
    },
    // 重新渲染
    async loadgetEmployeeList() {
      try {
        const res = await getEmployeeList(this.page, this.size)
        console.log('getEmployeeList', res)
        const List = res.data.rows
        this.EmployeeList = List
        this.total = res.data.total
        // 保存数据
      } catch (err) {
        console.log('getEmployeeList', err)
      }
    },

    // 分页功能
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page = 1
      this.size = val
      this.loadgetEmployeeList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.loadgetEmployeeList()
    }
  }
}
</script>
